home *** CD-ROM | disk | FTP | other *** search
- unit BldSql;
-
- interface
-
- uses
- Classes, SysUtils;
-
- procedure BuildSQL(ArticleType: string;
- AuthorName: string;
- IssueNumber: string;
- KeywordSelect: string;
- KeywordSearch: string;
- QueryText: TStrings);
-
- implementation
-
- procedure BuildSQL(ArticleType: string;
- AuthorName: string;
- IssueNumber: string;
- KeywordSelect: string;
- KeywordSearch: string;
- QueryText: TStrings);
- { Accepts the criteria values and returns the proper SQL statement in QueryText.
- Input values are expected to be empty strings if they are not used in the
- selection criteria. QueryText is expected to be properly allocated. }
- var
- QueryStr: String;
- Where: Boolean;
- I: Integer;
- CheckArticleType: Boolean;
- CheckAuthorName: Boolean;
- CheckIssueNumber: Boolean;
- CheckKeywordSelect: Boolean;
- CheckKeywordSearch: Boolean;
- begin
- CheckArticleType := ArticleType <> '';
- CheckAuthorName := AuthorName <> '';
- CheckIssueNumber := IssueNumber <> '';
- CheckKeywordSelect := KeywordSelect <> '';
- CheckKeywordSearch := KeywordSearch <> '';
- KeywordSearch := Uppercase(KeywordSearch);
- if CheckKeywordSelect and CheckKeywordSearch then
- CheckKeywordSearch := False;
-
- QueryText.Clear;
- QueryText.Add('SELECT ARTICLE.*');
-
- if CheckKeywordSelect and not CheckKeywordSearch
- then QueryText.Add('FROM ARTICLE, KEYWORD')
- else QueryText.Add('FROM ARTICLE');
-
- if CheckAuthorName then { look for three authors }
- begin
- for I := 1 to 3 do { "Author i" }
- begin
- if I = 1 then QueryText.Add('WHERE (')
- else QueryText.Add(') OR (');
- QueryText.Add(' (ARTICLE."Author '+IntToStr(I)+'" = '+AuthorName+')');
-
- if CheckArticleType then
- begin
- QueryText.Add(' AND');
- QueryText.Add(' (ARTICLE."Article Type" = "' + ArticleType + '")');
- end;
-
- if CheckIssueNumber then
- begin
- QueryText.Add(' AND');
- QueryText.Add(' (ARTICLE."Issue #" = ' + IssueNumber + ')');
- end;
-
- if CheckKeywordSearch then
- begin
- { The subquery used here is not supported by the 16-bit BDE }
- QueryText.Add(' AND');
- {$IFDEF WIN32}
- QueryText.Add(' (ARTICLE."Article ID" IN');
- QueryText.Add(' (SELECT DISTINCT Keyword."Article ID" FROM KEYWORD');
- QueryText.Add(' WHERE UPPER(Keyword) LIKE "%' + KeywordSearch + '%"))');
- {$ELSE}
- QueryText.Add(' (ARTICLE."Article ID" = KEYWORD."Article ID")');
- QueryText.Add(' AND');
- QueryText.Add(' (KEYWORD."Keyword" LIKE "%' + KeywordSearch + '%")');
- {$ENDIF}
- end;
-
- if (CheckKeywordSelect and not CheckKeywordSearch) then
- begin
- { do not use keyword list if key _search_ has been used }
- QueryText.Add(' AND');
- QueryText.Add(' (ARTICLE."Article ID" = KEYWORD."Article ID")');
- QueryText.Add(' AND');
- QueryText.Add(' (KEYWORD."Keyword" = "' + KeywordSelect + '")');
- end
- end;
- QueryText.Add(')');
- end
- else { don't look for authors }
- begin
- Where := False;
-
- if CheckKeywordSearch then
- begin
- {$IFDEF WIN32}
- QueryText.Add('WHERE (ARTICLE."Article ID" IN');
- QueryText.Add(' (SELECT DISTINCT Keyword."Article ID" FROM KEYWORD');
- QueryText.Add(' WHERE UPPER(Keyword) LIKE "%' + KeywordSearch + '%"))');
- {$ELSE}
- QueryText.Add(' (ARTICLE."Article ID" = KEYWORD."Article ID")');
- QueryText.Add(' AND');
- QueryText.Add(' (KEYWORD."Keyword" LIKE "%' + KeywordSearch + '%")');
- {$ENDIF}
- Where := True;
- end;
-
- if (CheckKeywordSelect and not CheckKeywordSearch) then
- begin
- { do not use keyword list if key _search_ has been used }
- QueryText.Add('WHERE (ARTICLE."Article ID" =');
- QueryText.Add(' KEYWORD."Article ID")');
- QueryText.Add(' AND (KEYWORD."Keyword" = "' + KeywordSelect + '")');
- Where := True;
- end;
-
- QueryStr := '';
- if CheckArticleType then
- begin
- if not Where then QueryStr := QueryStr + 'WHERE '
- else QueryStr := QueryStr + ' AND ';
- Where := True;
- QueryStr := QueryStr+'(ARTICLE."Article Type" = "'+ArticleType+'")'#13#10
- end;
-
- if CheckIssueNumber then
- begin
- if not Where then QueryStr := QueryStr + 'WHERE '
- else QueryStr := QueryStr + ' AND ';
- Where := True;
- QueryStr := QueryStr+'(ARTICLE."Issue #" = '+IssueNumber+')'#13#10
- end;
- QueryText.Add(QueryStr);
- end;
- QueryText.Add('ORDER BY ARTICLE."Issue #", ARTICLE."Page Number"');
- end;
-
- end.
-